
package com.wy17113.view.struts.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.wy17113.controller.utils.EncoderByMd5;
import com.wy17113.model.entity.User;
import com.wy17113.model.service.IUserService;

public class UserAction extends DispatchAction {	
	//登陆
	public ActionForward login(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HttpSession session = request.getSession(true);
		String randomCode = (String) session.getAttribute("randomCode");
		String reqRandom = request.getParameter("random");

		String respon = null;
		String account = request.getParameter("UserName");
		String pwd = request.getParameter("PassWord");

		ServletContext application = request.getSession().getServletContext();
		WebApplicationContext webContext = WebApplicationContextUtils
				.getRequiredWebApplicationContext(application);
		IUserService users = (IUserService) webContext.getBean("userService");

		if (null != randomCode && randomCode.equals(reqRandom)) {
			User user = null;
			try {
				user = users.userLogin(account, pwd);
				if (user == null) {
					request.setAttribute("message", "用户名密码错误!");
					respon = "login_no";
				} else {
					if(!user.getFlag().equals("0")){
						request.setAttribute("message", "该用户已被禁用,请联系管理员!");
						respon = "login_no";
					}else{
						session.setAttribute("lifeInfoUser", user);
						session.setAttribute("userId", user.getId());
						respon = "login_ok";
					}					
				}
			} catch (RuntimeException e) {
				e.printStackTrace();
				respon = "error";
			}			
		} else {
			request.setAttribute("message", "验证码错误!");
			respon = "login_no";
		}	
		return mapping.findForward(respon);
	}
	
	//用户注销登陆
	public ActionForward loginOut(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HttpSession session = request.getSession(true);
		session.invalidate();

		return mapping.findForward("exit");
	}
	
	//注册
	public ActionForward regist(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HttpSession session = request.getSession(true);
		String respon = null;
		SimpleDateFormat smt = new SimpleDateFormat("yyyy-MM-dd");
		
		String account = request.getParameter("UserName");
		String password = request.getParameter("PassWord");

		String randomCode = (String) session.getAttribute("randomCode");
		String reqRandom = request.getParameter("random");
		
		ServletContext application = request.getSession().getServletContext();
		WebApplicationContext webContext = WebApplicationContextUtils
				.getRequiredWebApplicationContext(application);
		IUserService users = (IUserService) webContext.getBean("userService");
		
		User user = new User();
		user.setAccount(account);
		user.setPassword(EncoderByMd5.getEncoderByMd5(password));
		user.setRegDate(smt.format(new Date()));
		user.setFlag("0");
		
		if (null != randomCode && randomCode.equals(reqRandom)) {
			if(users.checkAccountExist(account) != 1){
				try {
					users.saveUser(user);
					respon = "regist_ok";
					request.setAttribute("message", "恭喜您,注册成功!");
				} catch (Exception e) {
					respon = "regist_no";
					e.printStackTrace();
				}
			}else{
				request.setAttribute("message", "该帐号已经被占用，请使用其他帐号!");
				respon = "regist_no";
			}	
		}else{
			request.setAttribute("message", "验证码错误!");
			respon = "regist_no";
		}
			
		return mapping.findForward(respon);
	}
}
